package com.jimmy.medical.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import groovy.transform.stc.ClosureParams;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

import javax.persistence.*;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

/**
 * @author jimmy
 * @Desc: 服务工单类型 - 将订单拆分为若干工单
 * @date 2018-04-03
 */
@Entity
@Table(name = "flow_order_type")
public class FlowOrderType extends AbstractEntity implements Comparable<FlowOrderType> {

    /**
     * 类型名称
     */
    @Column(name = "type_name")
    private String typeName;

    /**
     * 类型代码
     */
    @Column(name = "type_code")
    private String typeCode;

    /**
     * 标识是否有子类型，有子类型时，价格和默认得分取子类型
     */
    @Column(name = "sub_type")
    private String subType;

    /**
     * 价格
     */
    @Column(name = "price", columnDefinition = "decimal(7,2)")
    private Double price;

    /**
     * 默认得分
     */
    @Column(name = "default_score")
    private Integer defaultScore;

    /**
     * 顺序
     */
    @Column(name = "type_order")
    private Integer typeOrder;

    /**
     * 最大步骤，用于流程控制，值为flowOrderTypes.size()
     */
    @Column(name = "max_step")
    private Integer maxStep;

    /**
     * 包含的服务项
     */
    @ManyToMany(cascade = {CascadeType.REMOVE}, fetch = FetchType.EAGER)
    @JoinTable(name = "flow_order_type_flow_order_service_item", joinColumns = {@JoinColumn(name = "flow_order_type_id", referencedColumnName = "id")}, inverseJoinColumns = {@JoinColumn(name = "flow_order_service_item_id", referencedColumnName = "id")})
    @OrderBy("itemOrder asc")
    private Set<FlowOrderServiceItem> flowOrderTypes = new TreeSet<FlowOrderServiceItem>();

    /**
     * 下一步流程
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "next_flow_order_type")
    @NotFound(action = NotFoundAction.IGNORE)
    private FlowOrderType next;

    @OneToMany
    @JoinColumn(name = "flow_order_type_id")
    @JsonIgnore
    private List<FlowOrderSubType> subTypeList;

    public String getTypeName() {
        return typeName;
    }

    public void setTypeName(String typeName) {
        this.typeName = typeName;
    }

    public String getTypeCode() {
        return typeCode;
    }

    public void setTypeCode(String typeCode) {
        this.typeCode = typeCode;
    }

    public String getSubType() {
        return subType;
    }

    public void setSubType(String subType) {
        this.subType = subType;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public Integer getDefaultScore() {
        return defaultScore;
    }

    public void setDefaultScore(Integer defaultScore) {
        this.defaultScore = defaultScore;
    }

    public Integer getTypeOrder() {
        return typeOrder;
    }

    public void setTypeOrder(Integer typeOrder) {
        this.typeOrder = typeOrder;
    }

    public Integer getMaxStep() {
        return maxStep;
    }

    public void setMaxStep(Integer maxStep) {
        this.maxStep = maxStep;
    }

    public Set<FlowOrderServiceItem> getFlowOrderTypes() {
        return flowOrderTypes;
    }

    public void setFlowOrderTypes(Set<FlowOrderServiceItem> flowOrderTypes) {
        this.flowOrderTypes = flowOrderTypes;
    }

    public FlowOrderType getNext() {
        return next;
    }

    public void setNext(FlowOrderType next) {
        this.next = next;
    }

    public List<FlowOrderSubType> getSubTypeList() {
        return subTypeList;
    }

    public void setSubTypeList(List<FlowOrderSubType> subTypeList) {
        this.subTypeList = subTypeList;
    }

    @Override
    public int compareTo(FlowOrderType o) {
        if(o != null) {
            if(getTypeOrder() > o.getTypeOrder()) {
                return 1;
            } else {
                return -1;
            }
        }
        return 0;
    }
}
